package gui.polish;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.text.html.HTMLDocument.Iterator;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JToggleButton;

import java.awt.FlowLayout;

import javax.swing.BoxLayout;

import clients.RPCClient;
import entities.StartPolishEntity;

import java.awt.ComponentOrientation;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.BorderLayout;
import java.util.ArrayList;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.border.BevelBorder;
import javax.swing.JSpinner;

import net.miginfocom.swing.MigLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextPane;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;

import java.awt.SystemColor;
import java.awt.Font;

import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;

import java.awt.Color;
/**
 * 
 * @author adam
 *This Panel shows the comments while the polish phase , including the option to extend the days of polish needed , it also show's all the orderID's included in the selected jarID .
 */
public class MyInfoPanel extends JPanel {
	private MyInfoPanel myInfoPanel;
	public JTable table;
	private RPCClient myRPCClient;
	private DefaultTableModel model ;
	private MyInfoFrame myInfoFrame;
	private ArrayList respond;
	private PolishPanel myPolishPanel;
	private String jarID;
	private String endDate;
	public JTextArea textAreaComments;
/**
 * 	Constructor
 * @param infoframe
 * @param a	- jar contents passed from the info frame
 * @param selectedColumnIndex
 * @param selectedRowIndex
 * @param myPolishPane
 * @param jarID
 * @param endDate
 */
	public MyInfoPanel(MyInfoFrame infoframe,ArrayList a,int selectedColumnIndex,int selectedRowIndex,PolishPanel myPolishPane,String jarID,String endDate) 
	{
		setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "\u05E4\u05E2\u05D5\u05DC\u05D5\u05EA \u05E0\u05D5\u05E1\u05E4\u05D5\u05EA", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(new Color(173, 216, 230));
		myInfoPanel = this;
		myInfoFrame=infoframe;
		respond=a;
		myPolishPanel=myPolishPane;
		this.jarID=jarID;
		this.endDate=endDate;
		
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("2px"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.LINE_GAP_ROWSPEC,
				RowSpec.decode("15px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("23px:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("50dlu"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JSeparator separator = new JSeparator();
		separator.setOrientation(SwingConstants.VERTICAL);
		add(separator, "5, 2, 1, 7, left, fill");
		
		JScrollPane scrollPane_1 = new JScrollPane();
		add(scrollPane_1, "3, 4, fill, fill");
		
		table = new JTable();
		table.setFont(new Font("Tahoma", Font.PLAIN, 15));
		scrollPane_1.setViewportView(table);
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u05E4\u05E7\"\u05E2\u05D5\u05EA", "\u05DB\u05DE\u05D5\u05EA"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		model = (DefaultTableModel) table.getModel();
		
		JPanel panel_1 = new JPanel(){@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		panel_1.setBackground(UIManager.getColor("InternalFrame.activeTitleBackground"));
		panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, SystemColor.inactiveCaption, null, null, null));
		add(panel_1, "7, 4, fill, fill");
		panel_1.setLayout(new MigLayout("", "[504px]", "[200px][33px]"));
		
		JScrollPane scrollPane = new JScrollPane();
		panel_1.add(scrollPane, "cell 0 0,grow");
		
		textAreaComments = new JTextArea();
		scrollPane.setViewportView(textAreaComments);
		textAreaComments.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		
		JButton btnAddComment = new JButton("הוסף הערה");
		btnAddComment.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnAddComment.setIcon(new ImageIcon(MyInfoPanel.class.getResource("/graphics/comment24.png")));
		btnAddComment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Object>message = new ArrayList<>();
				ArrayList<Object>respond = new ArrayList<>();
				message.add("updateCommentsIntoPolishRow");
				message.add(jarID);
				String comments = textAreaComments.getText().toString();
				model = (DefaultTableModel) table.getModel();
				String orderID = model.getValueAt(0, 0).toString();
				message.add(orderID);
				message.add(comments);
				try{
					myRPCClient = new RPCClient();
					respond = myRPCClient.call(message);
					if(respond.get(1).toString().equalsIgnoreCase("valid"))
					{
						// everything is fine , comments were updated .
						message.clear();
						message.add("");
					}
					else{
						//something is wrong .
					}
				}catch(Exception e1)
				{
					e1.printStackTrace();
				}
				
			}
		});
		panel_1.add(btnAddComment, "cell 0 1,growx,aligny center");
		
		JButton btnClose = new JButton("סגור ");
		btnClose.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnClose.setIcon(new ImageIcon(MyInfoPanel.class.getResource("/graphics/back.png")));
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					myInfoFrame.dispose();
			}
		});
		add(btnClose, "3, 6, fill, center");
		
		JPanel panel = new JPanel(){@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		panel.setForeground(SystemColor.inactiveCaption);
		panel.setBackground(UIManager.getColor("InternalFrame.activeTitleBackground"));
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, SystemColor.inactiveCaption, null, null, null));
		add(panel, "7, 6, 1, 2, fill, fill");
		panel.setLayout(null);
		
		JSpinner spinnerExtraTime = new JSpinner();
		spinnerExtraTime.setFont(new Font("Tahoma", Font.PLAIN, 13));
		spinnerExtraTime.setBounds(7, 10, 52, 27);
		panel.add(spinnerExtraTime);
		
		JButton btn_extraTime = new JButton("הוסף זמן");
		btn_extraTime.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btn_extraTime.setIcon(new ImageIcon(MyInfoPanel.class.getResource("/graphics/time24.png")));
		btn_extraTime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object[] options = { "כן", "לא" };
				int answer = JOptionPane.showOptionDialog(infoframe, "האם אתה בטוח שברצונך להאריך את פעולת הפוליש עבור הצנצנת שנבחרה?", "תוספת זמן", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[1]);
				if(answer == 0)
				{
					if(selectedColumnIndex != -1 && selectedRowIndex != -1)
					{
						ArrayList<Object>message = new ArrayList<>();
						ArrayList<Object>respond = new ArrayList<>();
						message.add("updateExtraDays");
						message.add(jarID);
						message.add(spinnerExtraTime.getValue().toString());	
						message.add(endDate);
						try{
							myRPCClient = new RPCClient();
							respond = myRPCClient.call(message);
							if(respond.get(1).toString().equals("valid"))
							{
								ArrayList<Object> message1 = new ArrayList<>();
								ArrayList<Object> respond1=null;
								message1.add("initializePolishPane");
								try{
									myRPCClient = new RPCClient();
									respond1 = myRPCClient.call(message1);
									if(respond1.get(1).equals("valid"))
										myPolishPanel.setVariables(respond1);
									else{
									} 
								}
								catch (Exception e1)
								{
									e1.printStackTrace();
								}
							}
							else{}
						}catch(Exception e1)
						{
							e1.printStackTrace();
						}		
					}	
				}
				else{}
			}
		});
		btn_extraTime.setBounds(72, 9, 118, 28);
		panel.add(btn_extraTime);
		initialize();

	}
/**
 * This method initializes the info panel table which holds the orders and quantity's 
 */
	public void initialize()
	{
		int i=0, i1=0;
			while (i1< respond.size())
			{
				model.addRow(new Object[]{respond.get(2+i*2) ,respond.get(3+i*2)});
				i++;
				i1=2*i+2;
			}

	}
	
	@Override
	public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Color color1 = getBackground();
    Color color2 = color1.darker();
    int w = getWidth();
    int h = getHeight(); 
    GradientPaint gp = new GradientPaint(
            0, 0, color1,
            0, h, color2);

    g2d.setPaint(gp);
    g2d.fillRect(0, 0, w, h);
    }
}
